import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 使用两个独立的数组分别管理动态路由和静态路由
// 所有的动态路由 不同的用户看到的数量不一致
import { asyncRoutes } from './asyncRoutes'

// 静态路由 每个用户都可以看到
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/Login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/import',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'import',
        component: () => import('@/views/ImportExcel/index.vue'),
        meta: { title: '导入' }
      }
    ]
  },
  {
    path: '/detail',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'detail',
        component: () => import('@/views/Employee/detail.vue'),
        meta: { title: '员工详情' }
      }
    ]
  }
]
console.log([...constantRoutes, ...asyncRoutes])

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    mode: 'history', // 路由模式 默认hash
    scrollBehavior: () => ({ y: 0 }), // 路由的滚动行为
    routes: [...constantRoutes]
  })

const router = createRouter() // 每次调用都会拿到一个新的router实例对象

// reset 路由方法
// 这个方法就是用来重置路由的方法 只要调用此方法就可以把先前的路由记录清空
export function resetRouter() {
  // 得到一个全新的router实例对象
  const newRouter = createRouter()
  // 使用新的路由记录覆盖掉老的路由记录
  router.matcher = newRouter.matcher // reset router
}

export default router
